package com.itany.shop.controller;

import com.itany.shop.entity.User;
import com.itany.shop.exception.CodeErrorException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserExistException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.UserService;
import com.itany.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author MissChen
 * @date 2024年05月16日 13:38
 */
@WebServlet("*.user")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        switch (path) {
            case "/register.user":
                register(request, response);
                break;
            case "/login.user":
                login(request, response);
                break;
            case "/logout.user":
                logout(request,response);
        }


    }

    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("----------注册------------");
        UserService userService = (UserService) ObjectFactory.getObject("userService");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String phone = request.getParameter("phone");
        String address = request.getParameter("address");
        try {
            userService.register(username, password, phone, address);
            response.sendRedirect(request.getContextPath() + "/jsp/login.jsp");
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            request.setAttribute("registerMsg", e.getMessage());
            request.getRequestDispatcher("/jsp/register.jsp").forward(request, response);
        } catch (UserExistException e) {
            e.printStackTrace();
            request.setAttribute("registerMsg", e.getMessage());
            request.getRequestDispatcher("/jsp/register.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("registerMsg", "服务器内部异常");
            request.getRequestDispatcher("/jsp/register.jsp").forward(request, response);
        }


    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("=========我在登录========");
        UserService userService= (UserService) ObjectFactory.getObject("userService");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code=request.getParameter("code");
        String imageCode= (String) request.getSession().getAttribute("code");
        try {
            User user=userService.login(username,password,code,imageCode);
            System.out.println(user);
            request.getSession().setAttribute("user",user);
            //当用户成功登录后，为当前用户分配一个新的购物车
            request.getSession().setAttribute("cart",new Cart());

            // response.sendRedirect(request.getContextPath()+"/jsp/main.jsp");
            response.sendRedirect(request.getContextPath()+"/findAll.product");
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (UserNotExistException e) {
            e.printStackTrace();
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (CodeErrorException e) {
            e.printStackTrace();
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("loginMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        }

    }

    public  void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("-----------注销------------");
        request.getSession().invalidate();
        // response.sendRedirect(request.getContextPath()+"/jsp/main.jsp");
        response.sendRedirect(request.getContextPath()+"/findAll.product");
    }
}
